package com.zzptc.syl.traffic;

import android.os.Bundle;

import androidx.fragment.app.Fragment;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class LeftFragment extends Fragment {
    private TextView number;
    private TextView no;
    private TextView buckle;
    private TextView fine;
    private final Retrofit retrofit=new Retrofit.Builder()
            .baseUrl("http://hh1.me:5001/")
            .addConverterFactory(GsonConverterFactory.create())
            .build();
    private List<Getcarno.DataBean.LogBean> logBeans;


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view= inflater.inflate(R.layout.fragment_left, container, false);
        setOnListener(AppData.getInstance().getCarno());
        number=view.findViewById(R.id.tv_number);
        no=view.findViewById(R.id.tv_no);
        buckle=view.findViewById(R.id.tv_buckle);
        fine=view.findViewById(R.id.tv_fine);
        return  view;
    }
    private void setOnListener(String car){
        Map<String,Object> data1=new HashMap<>();
        data1.put("CarNo","鲁"+car);
        Carservice carservice=retrofit.create(Carservice.class);
        Call<Getcarno> call=carservice.getweizhang(data1);
        call.enqueue(new Callback<Getcarno>() {
            @Override
            public void onResponse(Call<Getcarno> call, Response<Getcarno> response) {
                logBeans=response.body().getData().get(0).getLog();
                int k=0;
                int z=0;
                int f=0;
                number.setText(response.body().getData().get(0).getCarNo());
                for (int i =0;i<logBeans.size();i++){
                    if (logBeans.get(i).isProcessed()==false){
                        k++;
                    }
                    z=z+logBeans.get(i).getMark();
                    f=f+logBeans.get(i).getFee();
                }
                no.setText("未处理违章"+k+"次");
                buckle.setText("扣"+z+"分");
                fine.setText("罚款"+f+"元");
            }

            @Override
            public void onFailure(Call<Getcarno> call, Throwable t) {

            }
        });
    }
}